package com.jokelandave.poc.server.restlet;

import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import com.jokelandave.poc.client.domain.ClientTeam;
import com.jokelandave.poc.server.dao.TeamDao;
import com.jokelandave.poc.server.domain.Sport;
import com.jokelandave.poc.server.domain.Team;

public class TeamServerResource extends ServerResource {
	
	TeamDao teamService;
	
	public TeamServerResource()
	{
		setNegotiated(false);
	}

	@Override
	protected void doInit() throws ResourceException
	{
		teamService = new TeamDao();
		System.out.println("The init was performed");
	}
	
	@Override
	protected void doCatch(Throwable throwable)
	{
		System.out.println("An exception was thrown by the TeamServerResource");
	}
	
	@Override
	protected void doRelease() throws ResourceException
	{
		System.out.println("The TeamServerResource was released");
	}
	
	@Override 
	protected Representation get() throws ResourceException
	{	
		Team team = new Team();		
		team.setLocation("Minneapolis");
		team.setName("Vikings");
		team.setSport(Sport.NFL);
		
		Team serverTeam = teamService.putAndGet(team);
		
		ClientTeam toReturn = new ClientTeam();
		toReturn.setId(serverTeam.getId());
		toReturn.setName(serverTeam.getName());
		toReturn.setLocation(serverTeam.getLocation());
		toReturn.setSport(serverTeam.getSport().toString());
		
		return new JacksonRepresentation<ClientTeam>(toReturn);
	}
	
	@Override
	protected Representation options() throws ResourceException
	{
		System.out.println("The OPTIONS method of TeamServerResource was invoked");
		throw new RuntimeException("Not yet implemented");
	}
}
